package indigo.loader 
{
	import indigo.loader.events.LoadEvent;
	import indigo.loader.loaders.AbstractLoader;
	import indigo.loader.loaders.BulkLoader;
	import indigo.loader.loaders.LoadActor;
	import indigo.loader.types.LoadingStates;

	import flash.events.Event;
	/**
	 * The AssetManager is a singleton BulkLoader that extends the BulkLoader
	 * by keeping references to the loaded assets. 
	 * 
	 */
	final public class ContentManager extends BulkLoader
	{
		/**
		 * Reference to the AssetManager
		 */
		private static var INSTANCE:ContentManager = null;

		
		/**
		 * Constructor
		 * 
		 */
		public function ContentManager( singleton:SingletonEnforcer )
		{
			super( );
			this.initialize( );		
		}

		
		/**
		 * Executes when the AssetManager is instantiated
		 * 
		 */
		private function initialize():void
		{
			this.keepReferences = true;
		}

		
		/**
		 * Loads a single asset
		 * @param 	String that indicates the asset which will be loaded
		 * @param	Function that is executed when the asset responds 
		 * 
		 */
		public function loadAsset( filename:String, eventHandler:Function ):AbstractLoader
		{
			var myActor:LoadActor;
			var myLoader:AbstractLoader;
			
			if ( !this.hasAsset( filename ) )
			{
				myActor = new LoadActor( filename );
				myLoader = myActor.loader;
				
				this._actorList[ filename ] = myActor;
			}
			else
			{
				myActor = this._actorList[ filename ] as LoadActor;
				myLoader = myActor.loader;
			}
		
			// Add listeners
			myLoader.addEventListener( LoadEvent.INIT, eventHandler, false, 1, true );
			myLoader.addEventListener( LoadEvent.STOP, eventHandler, false, 1, true );	
			myLoader.addEventListener( LoadEvent.ERROR, eventHandler, false, 1, true );
			myLoader.addEventListener( LoadEvent.PROGRESS, eventHandler, false, 1, true );
			myLoader.addEventListener( LoadEvent.COMPLETE, eventHandler, false, 1, true );
			
			if ( myLoader.loadingState == LoadingStates.NOT_LOADED )
			{						
				myLoader.addEventListener( LoadEvent.COMPLETE, this.onAsset, false, 0, false );
				myLoader.load( filename );
			}
			
			return myLoader;
		}

		
		/**
		 * Executes when the content is loaded 
		 * @param 	Event which triggers the method
		 * 
		 */
		private function onAsset( event:LoadEvent ):void
		{
			// Remove listeners
			var myLoader:AbstractLoader = event.target as AbstractLoader;
			myLoader.removeEventListener( LoadEvent.COMPLETE, this.onAsset );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		override public function set keepReferences( value:Boolean ):void
		{
			value = true;
			super.keepReferences = value;
		}		

		
		/**
		 * Returns the instance of the AssetManager. When there is no reference it
		 * will create a new instance.	 
		 * @return	Instance of the AssetManager
		 * 
		 */
		public static function getInstance():ContentManager
		{
			if ( INSTANCE == null )
				INSTANCE = new ContentManager( new SingletonEnforcer );
			
			return INSTANCE;
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		override public function dispose( event:Event = null ):void
		{
			super.dispose( );
			INSTANCE = null;
		}		
	}
}


internal class SingletonEnforcer
{
}